var log = [];
function print(value) {
	log.push(value);
}
function spawnSpawnFunc(futureC) {
	print("3. nested spawn");
	print("8. result " + futureC.result());
}
function spawnedFunc(futureB) {
	print("2. start spawned fiber");
	futureC = new Future();
	new Future(spawnSpawnFunc,[futureC]);
	var result = futureB.result(); // should wait and then return 10
	
	print("5. finished spawned fiber with a value of " + result);
	return result + 5; // when this finishes it fulfills futureA that started it
}
function coroutineDemo() { // wait for a the user to fire an event on a control
	print("1. start demo");
	var futureB = new Future();
	var futureA = new Future(spawnedFunc,[futureB]); // spawn a function
	print("4. function has been spawned, but is not done: isDone = " + futureA.isDone());
	futureB.fulfill(10);
	print("6. spawned function has finished and thus fulfilled futureA");
	var finalResult = futureA.result(); // This future has already been fulfilled and should provide a value of 15
	print("7. demo finished with a result from futureA of " + finalResult);
	futureC.fulfill(3);
	print("9. demo really finished");
}
function runtest() { // starting point
	coroutineDemo();
}
function getLogOutput() {
	return log.join("\n");	
}
handler = {};
function waitForHandler() {
	var future = new Future();
	handler.onFire = future.fulfill;
	future.result();
	print("handler finished");
}